package com.eascs.crm.nsEditDetail.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.admin.o2o.internal.entity.LoanapplyExpand;
import com.eascs.admin.o2o.internal.service.LoanapplyExpandService;
import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.file.FileTypeConstants;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxConfirmButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;

/**
 * 内审信息编辑
 * 
 * @author yy
 *
 */
@RestController
@RequestMapping("/nsEditDetail")
public class NsEditDetailRestController extends BaseRestController {
	private static Logger logger = LoggerFactory.getLogger(NsEditDetailRestController.class);
	@Autowired
	private O2oLoanapplyService o2oLoanapplyService;
	@Autowired
	private LoanapplyExpandService loanapplyExpandService;

	/**
	 * @Description:
	 * 				<p>
	 *               信贷经理填写内审信息
	 *               </p>
	 * @author CXW
	 * @date 2017年8月16日 上午9:53:43
	 * @version V1.0
	 */
	@RequestMapping("/ns_view")
	public HtmlRestResult output(String loanapplyNo) {
		logger.info("提交内审信贷经理编辑内审信息loanapplyNo：{}", loanapplyNo);
		O2oLoanapplyEntity loan = o2oLoanapplyService.findById(loanapplyNo);
		LoanapplyExpand expand = loanapplyExpandService.queryLoanapplyNo(loanapplyNo);
		loan.setPaymentType("LDZJ");
		ViewContext.put("loan", loan);
		ViewContext.put("expand", expand);
		queryDataList(loanapplyNo, FileTypeConstants.DALIST_TYPE_DCWD, null);
		queryDataListNS(loanapplyNo, FileTypeConstants.DALIST_TYPE_NSFJ, null);
		return HtmlRestResult.html("nsEditDetail/ns_ptxdjl_view");
	}

	@RequestMapping("/ns_edit_view")
	public BaseRestResult<String> outPutEdit(String loanapplyNo) {
		logger.info("提交内审信贷经理编辑内审信息loanapplyNo：{}", loanapplyNo);
		O2oLoanapplyEntity loan = o2oLoanapplyService.findById(loanapplyNo);
		LoanapplyExpand expand = loanapplyExpandService.queryLoanapplyNo(loanapplyNo);
		loan.setPaymentType("LDZJ");
		ViewContext.put("loanapplay", loan);
		ViewContext.put("expand_e", expand);
		queryDataList(loanapplyNo, FileTypeConstants.DALIST_TYPE_DCWD, "NS_XDJL");
		queryDataListNS(loanapplyNo, FileTypeConstants.DALIST_TYPE_NSFJ, "NS_XDJL");
		return HtmlRestResult.html("nsEditDetail/ns_ptxdjl");
	}

	public void queryDataList(String loanapplyNo, String code, String type) {
		PageResult<Object[]> pageResult = o2oLoanapplyService.findO2oDataListEntityById(loanapplyNo, "'" + code + "'");
		String titles[] = { "附件类型", "文件名", "提交人", "提交时间", "更新时间" };
		Button[] titleButtons = new Button[] { null, ButtonHelper.createOpenButton(null, "/file/download?datalistno=") };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles);
		AjaxConfirmButton[] dealButton = null;
		if (type == null) {
			// dealButton = new AjaxConfirmButton[] { new AjaxConfirmButton("删除
			// ", "/loanrest/delete/") };
		} else if (type.equals("NS_XDJL")) {
			dealButton = new AjaxConfirmButton[] { new AjaxConfirmButton("删除 ", "/loanrest/delete/") };
		}
		paginationInfo.setMoreButtons(dealButton);
		paginationInfo.setTitleButtons(titleButtons);
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setBuildRowNumber(true);
		ViewContext.put("pageResult", paginationInfo);// 个人附件
	}

	public void queryDataListNS(String loanapplyNo, String code, String type) {
		PageResult<Object[]> pageResult = o2oLoanapplyService.findO2oDataListEntityById(loanapplyNo, "'" + code + "'");
		String titles[] = { "附件类型", "文件名", "提交人", "提交时间", "更新时间" };
		Button[] titleButtons = new Button[] { null, ButtonHelper.createOpenButton(null, "/file/download?datalistno=") };
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles);
		AjaxConfirmButton[] dealButton = null;
		if (type == null) {
			// dealButton = new AjaxConfirmButton[] { new AjaxConfirmButton("删除
			// ", "/loanrest/delete/") };
		} else if (type.equals("NS_XDJL")) {
			dealButton = new AjaxConfirmButton[] { new AjaxConfirmButton("删除 ", "/loanrest/delete/") };
		}
		paginationInfo.setMoreButtons(dealButton);
		paginationInfo.setTitleButtons(titleButtons);
		paginationInfo.setBuildPagingNavigation(false);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setBuildRowNumber(true);
		ViewContext.put("pageResultNS", paginationInfo);// 个人附件
	}
}